<template>
  <div class="layout-all">
    <control :config="config" />
    <div class="layout-header" style="float:left">
      <router-link to="./form/">
        <el-button
          type="primary"
          icon="el-icon-plus"
          style="margin-left:0px"
        >添加</el-button>
      </router-link>
    </div>
    <div style="float:left;margin-left:20px;color:red;margin-top:5px;font-size:20px;font-weight:bold;">
      温馨提示：用户等级请勿擅自修改
    </div>
    <div class="layout-content">
      <el-table
        :data="tableData"
        stripe
        highlight-current-row
        style="margin-top:50px"
      >
       <el-table-column label="等级:" prop="id" :fixed="isMobile()?'left':undefined" width="70">
          <template slot-scope="scope">
            <span>{{scope.row.id}}</span>
          </template>
      </el-table-column>
      <el-table-column label="会员折扣:" prop="discount">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.discount" />
            <span v-else>{{scope.row.discount}}</span>
          </template>
      </el-table-column>
      <el-table-column label="等级标题:" prop="title">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.title" />
            <span v-else>{{scope.row.title}}</span>
          </template>
      </el-table-column>
      <el-table-column label="升级消费额度:" prop="need_num">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.need_num" />
            <span v-else>{{scope.row.need_num}}</span>
          </template>
      </el-table-column>
      <el-table-column label="消费额(单订单):" prop="need_one">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.need_one" />
            <span v-else>{{scope.row.need_one}}</span>
          </template>
      </el-table-column>
      <el-table-column label="所需直推人数:" prop="need_man">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.need_man" />
            <span v-else>{{scope.row.need_man}}</span>
          </template>
      </el-table-column>
      <el-table-column label="直推人所需等级:" prop="child_level">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.child_level" />
            <span v-else>{{scope.row.child_level}}</span>
          </template>
      </el-table-column>
      <el-table-column label="所需团队人数:" prop="team_nan">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.team_nan" />
            <span v-else>{{scope.row.team_nan}}</span>
          </template>
      </el-table-column>
      <el-table-column label="所需团队消费额:" prop="team_num">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.team_num" />
            <span v-else>{{scope.row.team_num}}</span>
          </template>
      </el-table-column>
      <el-table-column label="团队管理奖励%:" prop="team_manage">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.team_manage" />
            <span v-else>{{scope.row.team_manage}}</span>
          </template>
      </el-table-column>
      <el-table-column :label="'级差奖励:('+(percentage==1?'%)':'元)')" prop="level_diff">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.level_diff" />
            <span v-else>{{scope.row.level_diff}}</span>
          </template>
      </el-table-column>
      <el-table-column :label="'同级奖励:('+(percentage==1?'%)':'元)')" prop="level_identical">
          <template slot-scope="scope">
            <input type="text" style="width:100px;" v-if="edit_index == scope.$index" v-model="scope.row.level_identical" />
            <span v-else>{{scope.row.level_identical}}</span>
          </template>
      </el-table-column>
      <el-table-column label="单独分销:" prop="is_commission">
         <template slot-scope="scope">
            <el-button
              :type="scope.row.is_commission === 1 ? 'primary' : 'success'"
              size="mini"
              @click="change_status(scope.row.id, 'is_commission' ,scope.row.is_commission)"
            >{{ scope.row.is_commission === 1 ? "是" : "否" }}</el-button>
          </template>
      </el-table-column>
      <el-table-column label="是否开启:" prop="status">、
        <template slot-scope="scope">
          <el-button
                :type="scope.row.status === 1 ? 'primary' : 'success'"
                size="mini"
                @click="change_status(scope.row.id, 'status' ,scope.row.status)"
              >{{ scope.row.status === 1 ? "是" : "否" }}</el-button>
        </template>
      </el-table-column>
        <el-table-column
          align="center"
          prop="telephone"
          :fixed="isMobile()?undefined:'right'"
          width="180"
          label="操作"
        >
          <template slot-scope="scope">   
            <el-button size="small" type="success" icon="el-icon-check" v-if="edit_index == scope.$index" @click="saveInfo(scope.row)" /> 
            <el-button size="small" type="primary" icon="el-icon-edit"  @click="editInfo(scope)"/>
            <router-link :to="'./form/' + scope.row.id">
              <el-button size="small" type="warning" icon="el-icon-right" />
            </router-link>

          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      :current-page="current_page"
      :page-size="per_page"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import control from '@/components/control/control'
export default {
  name:"user_level_index",
  components: { control },
  data() {
    return {
      edit_index:-1,
      percentage:0,
      tableField:{},
      config: [
        { type: 'text', placeholder: '请输入名称', field: 'title', val: '' }
      ],
      dateTime: '',
      value: '',
      searchWhere: {
        title: ''
      },
      tableData: [],
      total: 0,
      current_page: 1,
      per_page: 14
    }
  },
  activated(){
    if(this.$getUpdate()){
      this.onSearch()
    }
  },
  created() {
    this.onSearch()
  },
  methods: {
    editInfo (item) {
      if (this.edit_index != item.$index) {
        this.edit_index = item.$index
      } else {
        this.edit_index = -1
      }
      
    },
    saveInfo (item) {
      console.log(item)
      this.get('/admin/VipSetting/save_field', item, 'POST').then(e => {
        this.edit_index = -1
        this.onSearch()
      })
    },
    handleField(row,item){
      return row.item
    },
    // 查询
    handleSizeChange(val) {
      this.onSearch()
    },
    handleCurrentChange(val) {
      this.current_page = val
      this.onSearch()
    },
    onSearch() {
      const form = {}
      form.page = this.current_page
      form.map = this.searchWhere
      this.get('/admin/VipSetting/index', form, 'POST').then(e => {
        this.tableData = e.list.data
        this.tableField = e.field
        this.percentage = e.percentage
        this.per_page = e.list.per_page
        this.total = e.list.total
        this.current_page = e.list.current_page
      })
    },
    // 添加
    onAdd() {},
    // 编辑
    onEdit(id) {
      console.log(id)
    },
    // 修改状态
    change_status(id, field, val) {
      this.update(id, val, field, 'VipSetting').then(e => {
        e ? this.onSearch() : ''
      })
    },
    // 删除
    onDel(id) {
      this.$msg_confirm().then(e => {
        if (e) {
          this.get('/admin/VipSetting/del?id=' + id, this.form, 'POST').then(e => {
            this.onSearch()
          })
        }
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin-top: 20px;
  margin-left: 10px;
}
.layout-content {
  margin-top: 20px;
}
</style>>
